import 'package:first_flutter/pages/cart/cart_page.dart';
import 'package:first_flutter/pages/category/category_page.dart';
import 'package:first_flutter/pages/home/home_page.dart';
import 'package:first_flutter/pages/member/member_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _IndexPageState();
  }
}

class _IndexPageState extends State<IndexPage> {
  final List<BottomNavigationBarItem> bottomTabs = [
    const BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.home), label: "首页"),
    const BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.search), label: "分类"),
    const BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.shopping_cart), label: "购物车"),
    const BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.profile_circled), label: "会员中心")
  ];
  final List tabBodies = [const HomePage(), const CategoryPage(), const CartPage(), const MemberPage()];
  int currentIndex = 0;
  var currentPage;

  @override
  void initState() {
    currentPage = tabBodies[currentIndex];
    super.initState();
  }

  void onNavigationBarTab(index) {
    setState(() {
      currentIndex = index;
      currentPage = tabBodies[currentIndex];
    });
  }

  @override
  Widget build(BuildContext context) {
    print("bottomTabs==============>");
    print(bottomTabs);
    print(bottomTabs.length);
    return Scaffold(
      backgroundColor: const Color.fromRGBO(244, 245, 245, 1.0),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        items: bottomTabs,
        onTap: (index) => onNavigationBarTab(index),
      ),
      body: currentPage,
    );
  }
}
